package net.oschina.esb.util;

import net.oschina.esb.exception.AssertFailException;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * @author Arvin
 * @time 2017/12/3 20:16
 */
public abstract class AssertUtil {

    /** 默认的异常代码 */
    public static final int DEFAULT_ERROR_CODE = 400;

    public static void assertNotBlank(String value, int errorCode, String message) {
        if (StringUtils.isBlank(value)) {
            throw new AssertFailException(errorCode, message);
        }
    }

    public static void assertNotBlank(String value, String message) {
        assertNotBlank(value, DEFAULT_ERROR_CODE, message);
    }

    public static void assertNotNull(Object value, int errorCode, String message) {
        if (null == value) {
            throw new AssertFailException(errorCode, message);
        }
    }

    public static void assertNotNull(Object value, String message) {
        assertNotNull(value, DEFAULT_ERROR_CODE, message);
    }

    public static void assertNull(Object value, int errorCode, String message) {
        if (null != value) {
            throw new AssertFailException(errorCode, message);
        }
    }

    public static void assertNull(Object value, String message) {
        assertNull(value, DEFAULT_ERROR_CODE, message);
    }

    public static void assertNotEmpty(Collection<?> collection, int errorCode, String message) {
        if (collection == null || collection.isEmpty()) {
            throw new AssertFailException(errorCode, message);
        }
    }

    public static void assertNotEmpty(Collection<?> collection, String message) {
        assertNotEmpty(collection, DEFAULT_ERROR_CODE, message);
    }

    public static void assertTrue(boolean bool, int errorCode, String message) {
        if (!bool) {
            throw new AssertFailException(errorCode, message);
        }
    }

    public static void assertTrue(boolean bool, String message) {
        assertTrue(bool, DEFAULT_ERROR_CODE, message);
    }

    public static void assertFalse(boolean bool, int errorCode, String message) {
        if (bool) {
            throw new AssertFailException(errorCode, message);
        }
    }

    public static void assertFalse(boolean bool, String message) {
        assertFalse(bool, DEFAULT_ERROR_CODE, message);
    }

    public static void assertPageParam(int pageNo, int pageSize, int errorCode) {
        if (pageNo < 1 || pageSize < 1) {
            throw new AssertFailException(errorCode, "分页参数不正确[pageNo=" + pageNo + "], [pageSize=" + pageSize + "]");
        }
    }

    public static void assertPageParam(int pageNo, int pageSize) {
        assertPageParam(pageNo, pageSize, DEFAULT_ERROR_CODE);
    }
}
